from message.JudgeRes import *
import json


with open("./config.json", encoding='utf-8') as f:
    others_answer = json.load(f)["others_answer"]
with open("./config.json", encoding='utf-8') as f:
    use_model = json.load(f)["bot_set"]["use_model"]


class Talk:
    def __init__(self, chatbot, ws):
        self.chatbot = chatbot
        self.ws = ws

    def talk_to_user(self, rev, chat_mode):  # 这里可以DIY对私聊yes酱的操作
        msg = rev["raw_message"]
        sender = rev['user_id']
        group_id = ""

        res_flg = judge_res(msg, False, sender)

        if res_flg is False:
            if chat_mode:
                res_flg = self.chatbot.chat(msg)
                if res_flg[0]:
                    return res_flg[1]
                else:
                    return False
            else:
                return False

        else:
            return res_flg

    def talk_to_group_user(self, rev, chat_mode):  # 这里可以DIY对群聊中@yes酱的操作
        msg = rev["raw_message"]
        sender = rev['user_id']
        group_id = rev["group_id"]

        res_flg = judge_res(msg, True, sender)
        print(res_flg)
 
        if res_flg is False:
            print("judge_res res is false")
            if chat_mode:
                print("chat_mode is true")
                res_flg = self.chatbot.chat(msg)
                if res_flg[0]:
                    return res_flg[1]
                else:
                    return choice(others_answer)
            else:
                print("chat_mode is false")
                return choice(others_answer)

        else:
            print("judge_res res is ",res_flg)
            return res_flg

    # 对添加好友的操作
    def add_friends(self, rev):
        print(rev)
        sender = rev['user_id']
        msg = rev['comment'].split('回答:')[1]
        if_add = add_friend(sender, msg, self.ws)
        obj = {
            'isOK': if_add[0],
            'flag': rev['flag'],
            'friendsName': if_add[1]
        }
        return obj

    # # 这里可以DIY对群聊的操作
    # def group_action(self, rev):
    #     msg = rev["raw_message"]
    #     user_id = rev["user_id"]
    #     group_id = rev["group_id"]
    #     # --------------------------------------------------------------------------------------检测关键字禁言
    #     if_ban = detect_ban(msg, user_id, group_id, self.ws)
    #     if if_ban[0]:
    #         return if_ban[1]
    #     return choice(others_answer["no_answer"])
